import axios from 'axios'
import {
  requestSuccessFunc,
  requestFailFunc,
  responseSuccessFunc,
  responseFailFunc
} from './interceptors'

import config from './config'
const axiosInstance = axios.create(config)

// 注入请求拦截
axiosInstance
  .interceptors.request.use(requestSuccessFunc, requestFailFunc)

// 注入失败拦截
axiosInstance
  .interceptors.response.use(responseSuccessFunc, responseFailFunc)

export default function $axios(url, type, data, _options = {}) {
    const options = {
        url,
        method: type,
        ..._options
    }
    if (type === 'POST') {
        options.data = data
    } else if (type === 'GET') {
        options.params = data
    }
    return axiosInstance(options)
}
